package com.imnc.foodmap.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;

import com.actionbarsherlock.view.MenuItem;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.mapapi.map.RouteOverlay;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPlanNode;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.imnc.foodmap.R;
import com.imnc.foodmap.model.PoiInfoParcelable;
import com.imnc.foodmap.ui.base.BaseActivity;
import com.imnc.foodmap.view.EatMapPoiOverlay;

public class MapActivity extends BaseActivity {

	public static final String LOAD_TYPE = "type";
	public static final int MAIN_LOAD = 0;
	public static final int POI_LOAD = 1;

	private static final float DEFINE_ZOOM = 14;
	public static final int REQUEST_ROUTE_PLANNING = 1001;
	private MapView mMapView = null;
	private MapController mMapController = null;
	private LocationData mLocationData = null;
	private MyLocationOverlay mLocationOverlay = null;
	private ArrayList<MKPoiInfo> allPoiInfoParcelables = null;
	/** 搜索 **/
	private MKSearch mSearch = null;
	private MKSearchListener mSearchListener = null;
	/** 导航 **/
	private RouteOverlay routeOverlay = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);

		mLocationData = MainActivity.locationData;
		allPoiInfoParcelables = MainActivity.searchResultInfo;

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapController = mMapView.getController();

		mSearch = new MKSearch();
		mSearchListener = new SearchListener();
		mSearch.init(mApplication.getmBMapManager(), mSearchListener);

		routeOverlay = new RouteOverlay(this, mMapView);

		mMapView.setBuiltInZoomControls(true);
		mMapController.enableClick(true);
		mMapController.setZoom(DEFINE_ZOOM);

		mLocationOverlay = new MyLocationOverlay(mMapView);
		mLocationOverlay.setData(mLocationData);
		mMapView.getOverlays().add(mLocationOverlay);
		mLocationOverlay.enableCompass();
		mMapController.animateTo(new GeoPoint(
				(int) (mLocationData.latitude * 1e6),
				(int) (mLocationData.longitude * 1e6)));
		mLocationOverlay
				.setLocationMode(com.baidu.mapapi.map.MyLocationOverlay.LocationMode.NORMAL);

		EatMapPoiOverlay poiOverlay = new EatMapPoiOverlay(this, mMapView,
				mSearch);

		ArrayList<MKPoiInfo> poiInfos = new ArrayList<MKPoiInfo>();
		for (MKPoiInfo info : allPoiInfoParcelables) {
			poiInfos.add(info);
		}
		poiOverlay.setData(poiInfos);
		mMapView.getOverlays().add(poiOverlay);

		Intent intent = getIntent();
		int type = intent.getIntExtra(LOAD_TYPE, 1);
		if (type == MAIN_LOAD) {
			policy(intent);
		}

		mMapView.refresh();
	}

	@Override
	protected void onDestroy() {
		mMapView.destroy();
		super.onDestroy();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			this.finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		System.out.println(requestCode);
		switch (requestCode) {
		case MapActivity.REQUEST_ROUTE_PLANNING:

			if (data != null) {
				policy(data);
			}

			break;

		default:
			break;
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	
	private void policy(Intent data) {
		PoiInfoParcelable poiInfoParcelable = data
				.getParcelableExtra(MainActivity.MAP_ALL_POI_RESULT);

		MKPlanNode start = new MKPlanNode();
		start.pt = new GeoPoint((int) (mLocationData.latitude * 1e6),
				(int) (mLocationData.longitude * 1e6));
		MKPlanNode end = new MKPlanNode();
		end.pt = poiInfoParcelable.pt;
		mSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
		mSearch.transitSearch(null, start, end);
		mSearch.drivingSearch(null, start, null, end);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mMapView.onSaveInstanceState(outState);

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mMapView.onRestoreInstanceState(savedInstanceState);
	}

	//
	private class SearchListener implements MKSearchListener {
		@Override
		public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetDrivingRouteResult(MKDrivingRouteResult result,
				int iError) {

			if (iError != 0 || result == null) {
				return;
			}

			routeOverlay.setData(result.getPlan(0).getRoute(0));
			mMapView.getOverlays().remove(routeOverlay);
			mMapView.getOverlays().add(routeOverlay);
			mMapView.refresh();
		}

		@Override
		public void onGetPoiDetailSearchResult(int arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1,
				int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
			// TODO Auto-generated method stub

		}

	}
}
